package com.hxd.mall.widget

import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.fragment.app.FragmentActivity
import com.hxd.mall.R
import com.hxd.mall.utils.local.StackManageUtil
import com.hxd.mall.utils.open.FaskClickUitl
import com.hxd.mall.utils.open.KeyboardUtil

/**
 * base_title组件
 * Created by qishuai on 2018/7/23.
 */
class HxdTitleBarView(var context: FragmentActivity) {

    /*init {
        // 设置状态栏高度
        (context.findViewById<RelativeLayout>(R.id.layout_base_title_second).layoutParams as RelativeLayout.LayoutParams)
            .topMargin =
            if ((WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS and context.window.attributes.flags)
                == WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
            ) {
                HxdGlobalInfo.statusBarHeight
            } else {
                0
            }
    }*/

    // 左边
    // RootView
    private val mViewLeftRoot: View = context.findViewById(R.id.layout_title_left)
    // Left ImageView
    private val mIvLeftBack: ImageView = context.findViewById(R.id.iv_left_title_back)
    // Left TextView
    private val mTvLeftName: TextView = context.findViewById(R.id.tv_left_title_text)

    // 右边
    // RootView
    private val mViewRightRoot: View = context.findViewById(R.id.layout_title_right)
    // Right ImageView
    private val mIvRightBack: ImageView = context.findViewById(R.id.iv_right_title_back)
    // Right TextView
    private val mTvRightName: TextView = context.findViewById(R.id.tv_right_title_text)

    // Center TextView
    private val mTvCenterName: TextView = context.findViewById(R.id.tv_title_name)

    /**
     * 设置Title name
     */
    fun setTitleName(name: String): HxdTitleBarView {
        if (TextUtils.isEmpty(name)) {
            mTvCenterName.text = ""
        } else {
            mTvCenterName.text = name
        }

        return this
    }

    /**
     * title lift hidder
     */
    fun setTitleLeftHidden(): HxdTitleBarView {
        mViewLeftRoot.visibility = View.GONE
        return this
    }

    /**
     * title lift 只显示图片(默认为'返回'图标)
     */
    fun setTitleLeftJustImg(icon: Int = R.mipmap.title_back): HxdTitleBarView {
        mIvLeftBack.visibility = View.VISIBLE
        mIvLeftBack.setImageResource(icon)
        mTvLeftName.visibility = View.GONE

        return this
    }

    /**
     * title left 只显示文字
     */
    fun setTitleLeftJustText(text: String = ""): HxdTitleBarView {
        mIvLeftBack.visibility = View.GONE
        mTvLeftName.visibility = View.VISIBLE
        mTvLeftName.text = text

        return this
    }

    /**
     * title left set clicked listener
     */
    fun setOnLeftClickedListener(listener: View.OnClickListener): HxdTitleBarView {
        mViewLeftRoot.setOnClickListener(listener)
        return this
    }

    /**
     * 升级版本(多次单击处理、键盘隐藏)
     * title left set clicked listener
     */
    fun setOnLeftClickedListener(callback: () -> Unit): HxdTitleBarView {
        mViewLeftRoot.setOnClickListener {
            if (FaskClickUitl.isFastClick()) {
                return@setOnClickListener
            }
            KeyboardUtil.hideKeyBoardActivity(context)
            callback()
        }
        return this
    }

    /**
     * 升级版本(多次单击处理、键盘隐藏、关闭)
     * title left set clicked listener
     */
    fun setOnLeftClickedListenerCommon(): HxdTitleBarView {
        mViewLeftRoot.setOnClickListener {
            if (FaskClickUitl.isFastClick()) {
                return@setOnClickListener
            }
            KeyboardUtil.hideKeyBoardActivity(context)
            StackManageUtil.removeActivity(context)

            /**
             * 这里有一个bug，就是挡在修改手机号时，进入输入原手机号验证码界面，点击下一步然后进入输入手机号界面，
             * 再点击下一步进入验证码界面，然后点击返回， 回到输入手机号页面，然后再次点击返回回到验证码界面，
             * 这时候在验证码界面点击返回按钮时无反应
             *
             * 原因是StackManageUtil里重复添加一个activity时会覆盖，当里面的界面finish之后，再次回退到这一界面，
             * 调用removeActivity会找不到此activity，因此在这里在手动调用一遍context.finish()
             */
            context.finish()
        }
        return this
    }

    /**
     * title right 只显示图片(默认为'返回'图标)
     */
    fun setTitleRightJustImg(icon: Int = R.mipmap.title_back): HxdTitleBarView {
        mIvRightBack.visibility = View.VISIBLE
        mIvRightBack.setImageResource(icon)
        mTvRightName.visibility = View.GONE

        return this
    }

    /**
     * title right 只显示文字
     */
    fun setTitleRightJustText(text: String = ""): HxdTitleBarView {
        mIvRightBack.visibility = View.GONE
        mTvRightName.visibility = View.VISIBLE
        mTvRightName.text = text

        return this
    }

    /**
     * title right 文字颜色
     */
    fun setTitleRightTextColor(@ColorInt color: Int): HxdTitleBarView {
        mTvRightName.setTextColor(color)
        return this
    }

    /**
     * title right set clicked listener
     */
    fun setOnRightClickedListener(listener: View.OnClickListener): HxdTitleBarView {
        mViewRightRoot.setOnClickListener(listener)
        return this
    }

    /**
     * title right set clicked listener
     */
    fun setOnRightClickedListener(callback: () -> Unit): HxdTitleBarView {
        mViewRightRoot.setOnClickListener(View.OnClickListener {
            if (FaskClickUitl.isFastClick()) {
                return@OnClickListener
            }
            callback()
        })
        return this
    }

    fun getTitleTextView(): TextView = mTvCenterName
}